﻿<?php
//include_once("config.php");
include_once("simple_html_dom.php");

if(!preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $_POST['name'])){
	$error = "请输入正确的姓名";
}
else if(!preg_match('/\d{15}/', $_POST['number'])){
	$error = "请输入15位准考证号";
}else{

	$name = $_POST['name'];
	$number = $_POST['number'];

	$url = 'http://www.chsi.com.cn/cet/query?zkzh='.$number.'&xm='.$name;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_REFERER, "http://www.chsi.com.cn");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$result = curl_exec($ch);
	curl_close($ch);

	if(strpos($result, 'class="error')){
		$error = "输入有误";
	}
	else if(strpos($result, '无法找到')){
		$error = "输入有误";
	}else{
		$html=str_get_html($result);
		$table=$html->find('.cetTable',0);
		foreach($table->find('tr') as $row){
			$data[] = $row->find('td', 0);
		}
		$school = $data[1]->innertext;
		$type = $data[2]->innertext;
		$time = $data[4]->innertext;

		$gradeTotal = explode('<br />',$data[5]->innertext);

		foreach($gradeTotal as $row){
			$row = str_replace('666', '', $row);
			preg_match('/\d+/', $row, $t);
			$grade[] = $t[0];
		}
	}
}

$data = array(
	'error' => urlencode($error),
	'school' => urlencode($school),
	'name' => urlencode($name),
	'type' => urlencode($type),
	'time' => $time,
	'total' => $grade[0],
	'listen' => $grade[1],
	'read' => $grade[2],
	'write' => $grade[3]
);

echo $json = urldecode(json_encode($data));
?>